<script setup>
import {ref, reactive} from "vue";
import {addAddress, editAddress} from "@/api/order";
import message from "@/components/library/message";
const emit = defineEmits(['on-success'])
const dialogVisible = ref(false)



// eslint-disable-next-line no-unused-vars
const open = (address) => {
  dialogVisible.value = true
//   传人{}的时候清空，否则赋值
  if(address.id) {
    for(const key in formData) {
      formData[key] = address[key]
    }
  }else {
    for(const key in formData) {
      if(key !== 'isDefault') {
        formData[key] = ''
      }
    }
  }
  dialogVisible.value = true
}

// 表单
const formData = reactive({
  receiver: '',
  contact: '',
  provinceCode: '',
  cityCode: '',
  countyCode: '',
  address: '',
  postalCode: '',
  addressTags: '',
  isDefault: 0,
  fullLocation: '',
  id:''
})

// 选择地区
const changeCity = (data) => {
  formData.provinceCode = data.provinceCode
  formData.cityCode = data.cityCode
  formData.fullLocation = data.fullLocation
  formData.countyCode = data.countyCode
}

// eslint-disable-next-line no-unused-vars
const submit = () => {
  if(formData.id) {
    // eslint-disable-next-line no-unused-vars
    editAddress(formData).then(data=>{
      message({type:'success', text:'修改收货地址成功'})
      dialogVisible.value = false
      emit('on-success', formData)
    })
  }else {
    addAddress(formData).then(data=> {
      message({type:'sucess', text:'添加收货地址成功'})
      formData.id = data.result.id
      dialogVisible.value = false
      emit('on-success', formData)
    })
  }
}

</script>

<template>
  <XtxDialog :title="(formData.id ? '编辑' : '添加') + '收货地址'" v-model="dialogVisible">
    <!--  渲染到默认插槽  -->
    <div class="address-edit">
      <!--  表单    -->
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input class="input" v-model="formData.receiver" placeholder="请输入收货人"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input v-model="formData.contact" class="input" placeholder="请输入手机号"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity :fullLocation="formda.fullLocation" @change="changeCity" placeholder="请选择所在地区"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
          </div>
        </div>
      </div>
    </div>
    <!--  渲染到name=footer的具名插槽   -->
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-left: 20px">取消</XtxButton>
      <XtxButton type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>